package com.hz.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hz.web.entity.CreditCommitmentEntity;
import com.hz.web.entity.DepartmentEntity;
import com.hz.web.service.IcreditCommitmentService;
import com.hz.web.service.IdepartmentService;
/**
 * 信用承诺控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("creditCommitment")
public class CreditCommitmentController {

	@Resource
	private IcreditCommitmentService creditCommitmentService;
	
	@Resource
	private IdepartmentService departmentService;
	
	/**
	 * 查询部门信息和部门下的承诺信息
	 * @return json
	 */
	@RequestMapping("queryDepartmentAndCommitment")
	@ResponseBody
	public JSONObject queryDepartmentAndCommitment(){
		JSONObject json = new JSONObject();
		DepartmentEntity deparent = new DepartmentEntity();
		deparent.setIsDel("1");
		deparent.setType("3");
		//查询部门信息
		List<DepartmentEntity> ls = new ArrayList<DepartmentEntity>(); 
		ls=departmentService.queryDepartment(deparent, 0, 0);
		Map<String, Object> parmsMap = new HashMap<String, Object>();//存放参数
		parmsMap.put("isDel", "1");
		//循环部门信息查询承诺信息
		for (DepartmentEntity departmentEntity : ls) {
			parmsMap.put("departmentCode", departmentEntity.getId());
			List<CreditCommitmentEntity> list = new ArrayList<CreditCommitmentEntity>();
			//查询承诺数据集合
			list = creditCommitmentService.queryCreditCommitment(parmsMap);
			departmentEntity.setChildList(list);
		}
		json.put("list", ls);
		return json;
	}
	
	/**
	 * 查询信用承诺信息
	 * @param commitmentName  承诺名称
	 * @param commitmentCode  承诺编码
	 * @param departmentName  部门名称
	 * @param departmentCode  部门编码
	 * @param isDel 1启用 2删除
	 * @param page 当前页
	 * @param rows 每页显示的条数
	 * @return JSONObject格式
	 */
	@RequestMapping("queryCreditCommitment")
	@ResponseBody
	public JSONObject queryCreditCommitment(String sysId,String commitmentName,
			String commitmentCode, String departmentName,
			String departmentCode, String isDel,Integer page,Integer rows) {
		JSONObject json = new JSONObject();
		Map<String, Object> parmsMap = new HashMap<String, Object>();//存放参数
		parmsMap.put("sysId", sysId);
		parmsMap.put("commitmentName", commitmentName);
		parmsMap.put("commitmentCode", commitmentCode);
		parmsMap.put("departmentName", departmentName);
		parmsMap.put("departmentCode", departmentCode);
		parmsMap.put("isDel", isDel);
		List<CreditCommitmentEntity> list = new ArrayList<CreditCommitmentEntity>();
		
		if(page!=null && rows !=null){
			Integer index = (page-1)*rows;
			parmsMap.put("index", index);
			parmsMap.put("count", rows);
			//查询数据总条数
			int total = creditCommitmentService.queryCreditCommitmentCount(parmsMap);
			json.put("total", total);
		}
		//查询数据集合
		list = creditCommitmentService.queryCreditCommitment(parmsMap);
		json.put("rows", list);
		return json;
	}
	/**
	 * 添加信用承诺信息
	 * @param entity 信用承诺实体类参数
	 * @return JSONObject
	 */
	@RequestMapping("addCreditCommitment")
	@ResponseBody
	public JSONObject  addCreditCommitment(CreditCommitmentEntity entity){
		JSONObject json = new JSONObject();
		int row =-1;
		try {
			row=creditCommitmentService.addCreditCommitment(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		json.put("row", row);
		return json;
	}
	/**
	 * 修改信用承诺信息
	 * @param entity 信用承诺实体类参数
	 * @return JSONObject
	 */
	@RequestMapping("updateCreditCommitment")
	@ResponseBody
	public JSONObject updateCreditCommitment(CreditCommitmentEntity entity){
		JSONObject json = new JSONObject();
		int row =-1;
		try {
			row=creditCommitmentService.updateCreditCommitment(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		json.put("row", row);
		return json;
	}
}
